home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / smail-3.1.28 / util / dcasehost.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-05  |  3.2 KB  |  135 lines

  1. /* @(#)util/dcasehost.c    1.4 9/6/92 01:10:10 */
  2.  
  3. /*
  4.  *    Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
  5.  *    Copyright (C) 1992  Ronald S. Karr
  6.  * 
  7.  * See the file COPYING, distributed with smail, for restriction
  8.  * and warranty information.
  9.  */
  10.  
  11. /*
  12.  * dcasehost - downcase the host field of standard pathalias output
  13.  *
  14.  * Downcase the first field (non-while chars up to whitespace).
  15.  * In the common case of pathalias, this downcases the hostname
  16.  * while leaving the path untouched.
  17.  *
  18.  * If the -c option is used, then input is assumed to be of
  19.  * pathalias -c form:
  20.  *    cost hostname path
  21.  * and is converted to:
  22.  *    hostname path cost
  23.  * before downcase is done.
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include "defs.h"
  29.  
  30. char *program;            /* argv[0] from main */
  31.  
  32. char buf[BUFSIZ+1+1];        /* i/o line buffer */
  33. char buf2[BUFSIZ+1+1];        /* holder for field one switch (-c) */
  34. char buf3[BUFSIZ+1+1];        /* holder for other fields (-c) */
  35.  
  36. void
  37. main(argc, argv)
  38.     int argc;        /* arg count */
  39.     char *argv[];    /* args */
  40. {
  41.     char *f1_white;    /* first whitespace beyond field 1 */
  42.     int cflag = 0;    /* cflag == 1 ==> field switch */
  43.  
  44.     /*
  45.      * parse args
  46.      */
  47.     program = argv[0];
  48.     if ( argc > 2 ) {
  49.     fprintf(stderr, "%s: usage: %s [-c]\n", program, program);
  50.     exit(-1);
  51.     } else if (argc == 2) {
  52.     if (strcmp(argv[1], "-c") != 0) {
  53.         fprintf(stderr, "%s: usage: %s [-c]\n", program, program);
  54.         exit(-1);
  55.     }
  56.     cflag = 1;
  57.     }
  58.  
  59.     /*
  60.      * process input lines until EOF
  61.      */
  62.     while (fgets(buf, BUFSIZ, stdin) != NULL) {
  63.  
  64.     /*
  65.      * if -c, move the fields
  66.      */
  67.     if (cflag) {
  68.         char *f2_field;    /* first char of field 2 */
  69.         char *f_end;    /* last char */
  70.  
  71.         /* find first whitespace char beyond field 1 */
  72.         f1_white = strpbrk(&buf[strspn(buf, " \t\n")], " \t\n");
  73.  
  74.         /* find first char of field 2 */
  75.         if (f1_white != NULL) {
  76.         f2_field = &f1_white[strspn(f1_white, " \t\n")];
  77.         }
  78.  
  79.         /* find last char */
  80.         f_end = &buf[strlen(buf)-1];
  81.  
  82.         /* switch fields if firewall checks  allow it */
  83.         if (f1_white != NULL && *f1_white != '\0' &&
  84.         *f2_field != '\0' &&
  85.         f_end > buf && *f_end == '\n') {
  86.  
  87.             /* save field 1 with newline and NULL */
  88.             strncpy(buf2, buf, (int)(f1_white-buf));
  89.         buf2[f1_white-buf] = '\n';
  90.         buf2[f1_white-buf+1] = '\0';
  91.  
  92.         /* move field 2 and beyond to front of line */
  93.         strcpy(buf3, f2_field);
  94.         strcpy(buf, buf3);
  95.  
  96.         /*
  97.          * add field 1 on the end of new line
  98.          *
  99.          * use white space before field 1 to separate
  100.          * field 1 from the end of the line, if we can
  101.          */
  102.         if (buf2[0] == ' ' || buf2[0] == '\t') {
  103.             /* overwrite the old trailing newline */
  104.             strncpy(&buf[f_end-f2_field], buf2, f1_white-buf+2);
  105.             
  106.         } else {
  107.             /* separate old line end and field 1 with a tab */
  108.             buf[f_end-f2_field] = '\t';
  109.             strncpy(&buf[f_end-f2_field+1], buf2, f1_white-buf+2);
  110.         }
  111.         }
  112.     }
  113.  
  114.     /*
  115.      * downcase the first field
  116.      */
  117.     /* find first whitespace char beyond field 1 */
  118.     f1_white = strpbrk(&buf[strspn(buf, " \t\n")], " \t\n");
  119.     /* downcase field 1 if it exists */
  120.     if (f1_white != NULL) {
  121.         register char *p;    /* index */
  122.  
  123.         for (p=buf; p < f1_white; ++p) {
  124.         *p = lowercase(*p);
  125.         }
  126.     }
  127.  
  128.     /*
  129.      * print the resulting line
  130.      */
  131.     fputs(buf, stdout);
  132.     }
  133.     exit(0);
  134. }
  135.